package com.sgcc.ln.pmos.mobileapp.presenter;


import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.ln.pmos.common.base.BasePresenter;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.http.HttpRequestUtils;
import com.sgcc.ln.pmos.common.http.MvpNetCallback;
import com.sgcc.ln.pmos.mobileapp.bean.DefaultContactInfoBean;
import com.sgcc.ln.pmos.mobileapp.bean.QueryContactInfoBean;
import com.sgcc.ln.pmos.mobileapp.bean.StatusBean;
import com.sgcc.ln.pmos.mobileapp.contract.ContactInfoContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class ContactInfoPresenter extends BasePresenter<ContactInfoContract.View> implements ContactInfoContract.Presenter {

    @Override
    public void saveContactInfo(Map<String, Object> params) {
        HttpRequestUtils.getInstance().request(params, HttpInterConst.SAVECONTACTINFO, new MvpNetCallback<StatusBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
            }

            @Override
            public void onResponse(StatusBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if (response.getStatus() == 0 && "Success".equals(response.getMessage())) {
                        getView().saveContactInfoSuccess(response);
                    } else {
                        if (!TextUtils.isEmpty(response.getMessage())) {
                            ToastUtils.showShort(response.getMessage());
                        }
                    }
                }
            }
        });
    }

    @Override
    public void queryContactInfo() {
        Map<String, Object> params = new HashMap<>();
        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERYCONTACTINFO, new MvpNetCallback<QueryContactInfoBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
            }

            @Override
            public void onResponse(QueryContactInfoBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if (response.getStatus() == 0 && "Success".equals(response.getMessage())) {
                        getView().queryContactInfoSuccess(response);
                    } else {
                        if (!TextUtils.isEmpty(response.getMessage())) {
                            ToastUtils.showShort(response.getMessage());
                        }
                    }
                }
            }
        });
    }

    @Override
    public void queryDefaultContactInfo() {


        HttpRequestUtils.getInstance().request(new HashMap<>(), HttpInterConst.DEFAULT_CONTACT_INFO,
                new MvpNetCallback<DefaultContactInfoBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                    }

                    @Override
                    public void onResponse(DefaultContactInfoBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        if (response != null) {
                            if (response.getStatus() == 0 && "Success".equals(response.getMessage())) {
                                if (null != response.getData() && response.getData().size() > 0)
                                    getView().defaultContactInfoSuccess(response.getData().get(0));
                            }
                        }
                    }
                });

    }
}
